package com.pinyougou.manager.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.pinyougou.pojo.TbBrand;
import com.pinyougou.sellergoods.service.BrandService;
import entity.MyResult;
import entity.PageResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandContrller {

    @Reference
    private BrandService brandService;

    @RequestMapping("/findAll")
    public List<TbBrand> findAll() {
        return brandService.findAll();
    }

    @RequestMapping("/findByPage")
    public PageResult findAllByPage(int page, int rows) {
        return brandService.findByPage(page, rows);
    }

    /**
     * 查询+分页
     *
     * @return
     */
    @RequestMapping("/search")
    public PageResult search(@RequestBody TbBrand tbBrand, int page, int rows) {
        return brandService.findByPage(tbBrand, page, rows);
    }

    @RequestMapping("/save")
    public MyResult save(@RequestBody TbBrand tbBrand) {
        try {
            brandService.save(tbBrand);
            return new MyResult(true, "保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new MyResult(true, "保存失败");
        }
    }

    @RequestMapping("/queryById")
    public TbBrand queryById(Long id) {
        return brandService.selectOne(id);
    }

    @RequestMapping("/update")
    public MyResult update(@RequestBody TbBrand tbBrand) {
        try {
            brandService.update(tbBrand);
            return new MyResult(true, "更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new MyResult(true, "更新失败");
        }
    }

    @RequestMapping("/delete")
    public MyResult delete(Long[] ids) {
        try {
            brandService.delete(ids);
            return new MyResult(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new MyResult(true, "删除失败");
        }
    }

}
